#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using PLACEHR.Web.Utilities;
using PLACEHR.Web.Categories.Management.Forms;
using PLACEHR.Web.Categories.Business.Forms;
using System.Collections;
using Gizmox.WebGUI.Common.Resources;

#endregion

namespace PLACEHR.Web.Categories.Business.Controls
{
    public partial class LeaveApplicationControl : UserControl, IHostedApplication
    {
        private Employee employee = Global.CurrentUser;
        public LeaveApplicationControl()
        {
            InitializeComponent();
            InitialData();
            Retrieve();
        }

        private void InitialData()
        {
            colName.Text = LanLayer.GetTrans(122);
            colVacactionType.Text = LanLayer.GetTrans(123);
            colOffice.Text = LanLayer.GetTrans(124);
            colDepartment.Text = LanLayer.GetTrans(125);
            colOpeningBalance.Text = LanLayer.GetTrans(126);
            colHoursTake.Text = LanLayer.GetTrans(127);
            colClosingBalance.Text = LanLayer.GetTrans(128);

            colName1.Text = LanLayer.GetTrans(122);
            colType.Text = LanLayer.GetTrans(123);
            colStartDate.Text = LanLayer.GetTrans(129);
            colEndDate.Text = LanLayer.GetTrans(130);
            colLength.Text = LanLayer.GetTrans(127);
            colStatus.Text = LanLayer.GetTrans(131);
            colApproveUser.Text = LanLayer.GetTrans(132);
            colApproveDate.Text = LanLayer.GetTrans(133);
        }

        private void Retrieve()
        {
            var vacationbaseList = Global.Context.VacationBases.Where(o => o.Employee.EmployeeID == employee.EmployeeID && o.IsDeleted == false && o.IsCurrent);
            
            lvwVacationBaseData.Items.Clear();
            lvwVacationBaseData.BeginUpdate();
            int j = 1;
            foreach (var vacation in vacationbaseList.ToList())
            {
                ListViewItem lvwItem = new ListViewItem("");
                lvwItem.SubItems.AddRange(new string[8]);

               // ComboBoxItem item = cbbVacationType.SelectedItem as ComboBoxItem;
                //string typeCode = item.Value as string;
                var officeDepartment_lnk = vacation.Employee.OfficeDepartment_lnk.FirstOrDefault();

                var length = vacation.Employee.EmployeeVacations.Where(o => o.Status == "Approved" && o.OperateTime.HasValue && o.OperateTime.Value > DateTime.Today.AddDays(-DateTime.Today.Day + 1) && o.VacationTypeCode == vacation.VacationTypeCode).Sum(o => o.Length); //&& o.VacationTypeCode == typeCode

                lvwItem.SubItems[0].Text = j.ToString();
                lvwItem.SubItems[1].Text = vacation.Employee.EnglishName + "(" + vacation.Employee.ChineseName + ")";
                lvwItem.SubItems[2].Text = LanLayer.GetTrans(vacation.VacationType_lkp.Content_id.Value);
                lvwItem.SubItems[3].Text = officeDepartment_lnk == null ? "" : officeDepartment_lnk.Office.OfficeName;
                lvwItem.SubItems[4].Text = officeDepartment_lnk == null ? "" : officeDepartment_lnk.Department.DepartmentName;
                lvwItem.SubItems[5].Text = vacation.InitialHours.HasValue ? vacation.InitialHours.Value.ToString("0.00") : "";
                lvwItem.SubItems[6].Text = length.HasValue ? length.Value.ToString("0.00") : "";
                lvwItem.SubItems[7].Text = vacation.FinalHours.HasValue ? vacation.FinalHours.Value.ToString("0.00") : "";

                lvwVacationBaseData.Tag = vacation;

                lvwVacationBaseData.Items.Add(lvwItem);
                j++;
            }

            var vacationList = Global.Context.EmployeeVacations.Where(o => o.IsDeleted == false && o.EmployeeID==employee.EmployeeID);
          
            lvwData.Items.Clear();
            lvwData.BeginUpdate();
            int i = 1;
            foreach (var vacation in vacationList)
            {
                ListViewItem lvwItem = new ListViewItem("");
                lvwItem.SubItems.AddRange(new string[9]);
                lvwItem.SubItems[0].Text = i.ToString();
                lvwItem.SubItems[1].Text = vacation.Employee.EnglishName + "(" + vacation.Employee.ChineseName + ")";
                lvwItem.SubItems[2].Text = LanLayer.GetTrans(vacation.VacationType_lkp.Content_id.Value);
                lvwItem.SubItems[3].Text = vacation.FromTime.HasValue? vacation.FromTime.Value.ToString("yyyy-MM-dd hh:mm:ss") : "";
                lvwItem.SubItems[4].Text = vacation.EndTime.HasValue ? vacation.EndTime.Value.ToString("yyyy-MM-dd hh:mm:ss") : "";
                lvwItem.SubItems[5].Text = vacation.Length.HasValue ? Utility.ConvertLengthToString(vacation.Length.Value) : "";
                lvwItem.SubItems[6].Text = vacation.Status;
                lvwItem.SubItems[7].Text = vacation.Employee1 == null ? "": vacation.Employee1.EnglishName + "(" + vacation.Employee1.ChineseName + ")";
                lvwItem.SubItems[8].Text = vacation.OperateTime.HasValue ? vacation.OperateTime.Value.ToString("yyyy-MM-dd hh:mm:ss") : "";

                lvwItem.Tag = vacation;

                lvwData.Items.Add(lvwItem);
                i++;
            }
        }

   

        private void lvwVacationBaseData_DoubleClick(object sender, EventArgs e)
        {
            //if (lvwData.SelectedItem != null)
            //{
            //    var currItem = lvwVacationBaseData.SelectedItem.Tag as EmployeeVacation;
            //    LeaveApplication form = new LeaveApplication(currItem);
            //    form.ShowDialog();
            //    form.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(form_FormClosed);
            //}
            //form.currItem = 
        }

        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            Retrieve();
        }

        public void InitializeApplication()
        {
            
        }

        public HostedToolBarElement[] GetToolBarElements()
        {
            ArrayList objElements = new ArrayList();
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(140), LanLayer.GetTrans(140), new IconResourceHandle("Outlook.NewItem.gif"), "Add"));
            objElements.Add(new HostedToolBarSeperator());
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(23), LanLayer.GetTrans(23), new IconResourceHandle("CRM.ico_16_5010.gif"), "DeleteSelected"));
            objElements.Add(new HostedToolBarSeperator());
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(24), LanLayer.GetTrans(24), new IconResourceHandle("CRM.ico_16_5006.gif.gif"), "Print"));
            //objElements.Add(new HostedToolBarSeperator());
            //objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(23), LanLayer.GetTrans(23), new IconResourceHandle("CRM.ico_16_5010.gif"), "DeleteSelected"));

            return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
        }

        public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
        {
            string strAction = (string)objButton.Tag;
            switch (strAction)
            {
                case "Add":
                    Add();
                    break;
                case "DeleteSelected":
                    DeleteSelected();
                    break;
                case "Print":
                    Print();
                    break;
                
            }
        }

        private void Print()
        {
            if (lvwData.SelectedItem != null)
            {
                EmployeeVacation e = lvwData.SelectedItem.Tag as EmployeeVacation;
                PrintVacationForm form = new PrintVacationForm();
                form.currItem = e;
                form.ShowDialog();
            }
        }

        private void DeleteSelected()
        {
            if (lvwData.SelectedItem != null)
            {
                EmployeeVacation e = lvwData.SelectedItem.Tag as EmployeeVacation;

                if (e.Status == "Pending")
                {
                    Global.Context.EmployeeVacations.DeleteObject(e);
                    Global.Context.SaveChanges();
                    lvwData.Items.Remove(lvwData.SelectedItem);
                }
                else
                {
                    MessageBox.Show(LanLayer.GetTrans(193));
                }
            }

        }

        private void Add()
        {
            LeaveApplication form = new LeaveApplication(null);
            form.ShowDialog();
            form.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(form_FormClosed);
        }

        private void lvwData_DoubleClick(object sender, EventArgs e)
        {
            if (lvwData.SelectedItem != null)
            {
                var currItem = lvwData.SelectedItem.Tag as EmployeeVacation;
                ApproveLeaveForm form = new ApproveLeaveForm(currItem, false);
                form.ShowDialog();
                form.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(form_FormClosed);
            }
        }
    }
}